package com.xuerhua.yxt.controller.sys;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.xuerhua.yxt.reqVo.SysDictTypeReqVO;
import com.xuerhua.yxt.utils.BeanUtils;
import com.xuerhua.yxt.utils.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xuerhua.yxt.common.Constants;
import com.xuerhua.yxt.common.GlobalMap;
import com.xuerhua.yxt.common.RspResult;
import com.xuerhua.yxt.common.enums.Status;
import com.xuerhua.yxt.controller.BaseController;
import com.xuerhua.yxt.model.Grid;
import com.xuerhua.yxt.model.SchoolCommentWeek;
import com.xuerhua.yxt.model.SysCommentTemplate;
import com.xuerhua.yxt.model.SysDictData;
import com.xuerhua.yxt.model.SysDictType;
import com.xuerhua.yxt.page.OrderBy;
import com.xuerhua.yxt.page.Page;
import com.xuerhua.yxt.service.ISchoolCommentWeekService;
import com.xuerhua.yxt.service.ISysCommentTemplateService;
import com.xuerhua.yxt.service.ISysDictDataService;
import com.xuerhua.yxt.service.ISysDictTypeService;
import com.xuerhua.yxt.service.ITermInfoService;
import com.xuerhua.yxt.vo.SysCommentTemplateQueryVO;
import com.xuerhua.yxt.vo.SysDictDataQueryVO;

@RequestMapping("sys/dict")
@Controller
public class DictController extends BaseController {

    private Logger logger = LoggerFactory.getLogger(DictController.class);

    @Autowired
    private ISysDictTypeService sysDictTypeService;
    @Autowired
    private ISysDictDataService sysDictDataService;
    @Autowired
    private ISysCommentTemplateService sysCommentTemplateService;
    @Autowired
    private ISchoolCommentWeekService schoolCommentWeekService;
    @Autowired
    private ITermInfoService termInfoService;

    @RequestMapping(value = {"", "/"})
    public String index() {
        return "sys/dictIndex";
    }

    @RequestMapping("commentTemplate")
    public String commentTemplate() {
        return "sys/commentTemplate";
    }

    @RequestMapping("weekTemplate")
    public String weekTemplate() {
        return "sys/weekTemplate";
    }

    @RequestMapping("dictType/list")
    @ResponseBody
    Object dictTypeList(SysDictType dictType) {
        try {
            return sysDictTypeService.selectListForParam(dictType);
        } catch (Exception e) {
            logger.error("--dictTypeList error={}", e);
        }
        return null;
    }

    @RequestMapping("dictData/page")
    @ResponseBody
    Object dictDataPage(SysDictDataQueryVO vo) {
        try {
            Page<SysDictData> page = new Page<SysDictData>();
            Grid grid = new Grid();
            List<OrderBy> sorts = new ArrayList<>();
            sorts.add(new OrderBy("CREATE_TIME"));
            page.setSorts(sorts);
            page.setPageNo(vo.getPage());
            page.setPageSize(vo.getRows());
            page.setParams(vo);
            List<SysDictData> list = sysDictDataService.selectListByPage(page);
            grid.setRows(list);
            grid.setTotal(page.getTotalRecord());
            return grid;
        } catch (Exception e) {
            logger.error("--dictDataPage error={}", e);
        }
        return null;
    }

    @RequestMapping("weekTemplate/list")
    @ResponseBody
    Object weekTemplateList(SchoolCommentWeek record) {
        try {
            return schoolCommentWeekService.selectListByParam(record);
        } catch (Exception e) {
            logger.error("--weekTemplateList error={}", e);
        }
        return null;
    }

    @RequestMapping("dictType/addUpdate")
    @ResponseBody
    Object dictTypeAddUpdate(SysDictType dictType) {
        try {
            if (dictType.getId() != null) {
                dictType.setUpdateTime(new Date());
                sysDictTypeService.updateByPrimaryKeySelective(dictType);
            } else {
                sysDictTypeService.insertSelective(dictType);
            }
            return new RspResult(Status.SUCCESS);
        } catch (Exception e) {
            logger.error("--dictTypeAddUpdate, error={}", e);
        }
        return new RspResult(Status.FAIL);
    }

    @RequestMapping("dictData/addUpdate")
    @ResponseBody
    Object dictDataAddUpdate(SysDictData dictData) {
        try {
            if (dictData.getId() != null) {
                dictData.setUpdateTime(new Date());
                sysDictDataService.updateByPrimaryKeySelective(dictData);
            } else {
                sysDictDataService.insertSelective(dictData);
            }
            if (dictData.getDictTypeId().intValue() == 4) {
                GlobalMap.put(Constants.SYS_CONF, sysDictDataService.selectDictData("sys_conf"));
            }
            return new RspResult(Status.SUCCESS);
        } catch (Exception e) {
            logger.error("--dictDataAddUpdate, error={}", e);
        }
        return new RspResult(Status.FAIL);
    }

    @RequestMapping("commentTemplate/page")
    @ResponseBody
    Object commentTemplatePage(SysCommentTemplateQueryVO vo) {
        try {
            Page<SysCommentTemplate> page = new Page<SysCommentTemplate>();
            Grid grid = new Grid();
            List<OrderBy> sorts = new ArrayList<>();
            sorts.add(new OrderBy("CREATE_TIME"));
            page.setSorts(sorts);
            page.setPageNo(vo.getPage());
            page.setPageSize(vo.getRows());
            page.setParams(vo);
            List<SysCommentTemplate> list = sysCommentTemplateService.selectListByPage(page);
            grid.setRows(list);
            grid.setTotal(page.getTotalRecord());
            return grid;
        } catch (Exception e) {
            logger.error("--commentTemplatePage error={}", e);
        }
        return null;
    }

    @RequestMapping("commentTemplate/addUpdate")
    @ResponseBody
    Object commentTemplateAddUpdate(SysCommentTemplate record) {
        try {
            if (record.getId() != null) {
                record.setUpdateTime(new Date());
                sysCommentTemplateService.updateByPrimaryKeySelective(record);
            } else {
                sysCommentTemplateService.insertSelective(record);
            }
            return new RspResult(Status.SUCCESS);
        } catch (Exception e) {
            logger.error("--commentTemplateAddUpdate, error={}", e);
        }
        return new RspResult(Status.FAIL);
    }

    @RequestMapping("weekTemplate/addUpdate")
    @ResponseBody
    Object weekTemplateAddUpdate(SchoolCommentWeek record) {
        try {
            if (record.getId() != null) {
                record.setUpdateTime(new Date());
                schoolCommentWeekService.updateByPrimaryKeySelective(record);
            } else {
                record.setTermId(getCurrentTerm().getId());
                schoolCommentWeekService.insertSelective(record);
            }
            return new RspResult(Status.SUCCESS);
        } catch (Exception e) {
            logger.error("--weekTemplateAddUpdate, error={}", e);
        }
        return new RspResult(Status.FAIL);
    }

    @RequestMapping("dictCode/exists")
    @ResponseBody
    int distCodeExists(String dictCode) {
        return sysDictTypeService.selectCountByDictCode(dictCode);
    }

    @RequestMapping("dictType/id")
    @ResponseBody
    Object dictTypeById(SysDictType sysDictType) {
        return sysDictTypeService.selectByPrimaryKey(sysDictType);
    }

    @RequestMapping("dictData/id")
    @ResponseBody
    Object dictDataById(SysDictData sysDictData) {
        return sysDictDataService.selectByPrimaryKey(sysDictData);
    }

    @RequestMapping("commentTemplate/id")
    @ResponseBody
    Object commentTemplateById(Integer id, Integer schoolId) {
        return sysCommentTemplateService.selectByPrimaryKey(id, schoolId);
    }

    @RequestMapping("weekTemplate/id")
    @ResponseBody
    Object weekTemplateById(Integer id) {
        return schoolCommentWeekService.selectByPrimaryKey(id);
    }

    @RequestMapping("dictData/dictCode")
    @ResponseBody
    Object dictDataDictCode(SysDictTypeReqVO vo) {
        //空则进去
        if (!(StringUtil.isNotNullOrZero(vo.getSchoolId()))) {
            //家长端用这个
            if (vo.getType().equals("xs")) {
                vo.setSchoolId(getAppUser().getParent().getSchoolId());
            } else {
                vo.setSchoolId(getAppUser().getTeacher().getSchoolId());
            }
        }
        SysDictType sysDictType = BeanUtils.copyAs(vo, SysDictType.class);
        return sysDictDataService.selectListByDictCode(sysDictType);
    }

    @RequestMapping("termList")
    @ResponseBody
    Object termList(int schoolId) {
        return termInfoService.selectList(schoolId);
    }
}
